package android.Spider;

import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;

public class SpiderPreferences {

	public static final String s_strPreferences = "Spider";
	public static final String s_strProfileInitialized = "ProfileInit";
	public static final String s_strUserFirstName = "UserFirstName";
	public static final String s_strUserLastName = "UserLastName";
	public static final String s_strPassword = "Password";
	public static final String s_strInterests = "Interests";
	public static final String s_strOccupation = "Occupation";
	public static final String s_strTown = "Town";
	public static final String s_strCountry = "Country";
	public static final String s_strGender = "Gender";
	public static final String s_strAvatar = "Avatar";

	private SpiderPreferences(Context ctx)
	{
		if (m_settings == null)
		{
			m_settings = ctx.getSharedPreferences(s_strPreferences, 0);		// 0 means MODE_PRIVATE
			GetUserProfile();
		}
	}
	
	public static SpiderPreferences Instance(Context ctx)
	{
		if (m_this == null)
			m_this = new SpiderPreferences(ctx);
		
		return m_this;
	}
	
	long GetRequestID()
	{
		if (m_idRequest == -1)
			m_idRequest = m_settings.getLong(SpiderListener.s_strReqID, 0);
		
		return m_idRequest;
	}
	
	void SetRequestID(long id)
	{
		if (id != GetRequestID())
		{
			m_idRequest = id;

			SharedPreferences.Editor edit = m_settings.edit();
			edit.putLong(SpiderListener.s_strReqID, id);
			edit.commit();
		}
	}
	
	boolean IsProfileInitialized() {
		if (m_idProfile == -1)
			m_idProfile = m_settings.getInt(s_strProfileInitialized, 0);
		
		return m_idProfile != 0;
	}
	
	int GetProfileID()
	{
		if (m_idProfile == -1)
			m_idProfile = m_settings.getInt(s_strProfileInitialized, 0);
		
		return m_idProfile;
	}
	
	int GetProfileCompleteness()
	{
		int nCount = 0;

		if (!m_usrProfile.m_strFirstName.equals(""))
			++nCount;
		
		if (!m_usrProfile.m_strLastName.equals(""))
			++nCount;
		
		if (!m_usrProfile.m_strPassword.equals(""))
			++nCount;
		
		if (m_usrProfile.m_nInterests.intValue() != 0)
			++nCount;
		
		if (!m_usrProfile.m_strTown.equals(""))
			++nCount;

		if (m_usrProfile.m_bMale != null)
			++nCount;
		
		return nCount * 100 / 6;
	}
	
	void SetProfileID(int id)
	{
		if (id != GetProfileID())
		{
			m_idProfile = id;
			SetPreference(s_strProfileInitialized, m_idProfile);
		}
	}
	
	void SetUserFirstName(String strName)
	{
		m_usrProfile.m_strFirstName = new String(strName);
		SetPreference(s_strUserFirstName, m_usrProfile.m_strFirstName);
	}

	void SetUserLastName(String strName)
	{
		m_usrProfile.m_strLastName = new String(strName);
		SetPreference(s_strUserLastName, m_usrProfile.m_strLastName);
	}

	void SetPassword(String strName)
	{
		m_usrProfile.m_strPassword = new String(strName);
		SetPreference(s_strPassword, m_usrProfile.m_strPassword);
	}
	
	void SetInterests(int nInterests)
	{
		m_usrProfile.m_nInterests = new Integer(nInterests);
		SetPreference(s_strInterests, nInterests);
	}
	
	void SetOccupation(int nOccupation)
	{
		m_usrProfile.m_nOccupation = new Integer(nOccupation);
		SetPreference(s_strOccupation, nOccupation);
	}
	
	void SetCountry(int nCountry)
	{
		m_usrProfile.m_nCountry = new Integer(nCountry);
		SetPreference(s_strCountry, nCountry);
	}
	
	void SetTown(String strTown)
	{
		m_usrProfile.m_strTown = strTown;
		SetPreference(s_strTown, strTown);
	}

	void SetGender(Boolean bMale)
	{
		m_usrProfile.m_bMale = bMale;

		SharedPreferences.Editor edit = m_settings.edit();
		edit.putBoolean(s_strGender, bMale);
		edit.commit();
	}

	void SetAvatar(int nAvatar)
	{
		m_usrProfile.m_nAvatar = nAvatar;
		SetPreference(s_strAvatar, nAvatar);
	}

	private void SetPreference(String strTo, String strValue)
	{
		SharedPreferences.Editor edit = m_settings.edit();
		edit.putString(strTo, strValue);
		edit.commit();
	}

	private void SetPreference(String strTo, int nValue)
	{
		SharedPreferences.Editor edit = m_settings.edit();
		edit.putInt(strTo, nValue);
		edit.commit();
	}

	public ProfileUser GetUserProfile()
	{
		m_usrProfile.m_strFirstName = m_settings.getString(s_strUserFirstName, "");
		m_usrProfile.m_strLastName = m_settings.getString(s_strUserLastName, "");
		m_usrProfile.m_strPassword = m_settings.getString(s_strPassword, "");
		
		m_usrProfile.m_nInterests = m_settings.getInt(s_strInterests, m_usrProfile.m_nInterests);
		m_usrProfile.m_nOccupation = m_settings.getInt(s_strOccupation, m_usrProfile.m_nOccupation);
		m_usrProfile.m_nCountry = m_settings.getInt(s_strCountry, m_usrProfile.m_nCountry);
		m_usrProfile.m_strTown = m_settings.getString(s_strTown, "");
		m_usrProfile.m_bMale = m_settings.getBoolean(s_strGender, false);
		m_usrProfile.m_nAvatar = m_settings.getInt(s_strAvatar, m_usrProfile.m_nAvatar);
		
		return m_usrProfile;
	}
	
	public ProfileUser FindProfile(String strAccount)
	{
		ProfileUser profile = new ProfileUser();
		if (m_lstAllProfiles != null)
		{
			synchronized (m_lstAllProfiles) {
				for (int i = 0; i < m_lstAllProfiles.size(); ++i)
				{
					if (m_lstAllProfiles.get(i).m_strGMail.equals(strAccount))
						profile = m_lstAllProfiles.get(i);
				}
			} 
		}
		
		return profile;
	}
	
	public void SetAllProfiles(ArrayList<ProfileUser> lstProfiles)
	{
		if (m_lstAllProfiles == null)
		{
			m_lstAllProfiles = lstProfiles;
		}
		else
		{
			synchronized (m_lstAllProfiles) {
				m_lstAllProfiles = lstProfiles;
			} 
		}
	}
	
	// Members which this preference is storing.
	private long m_idRequest = -1;
	private int m_idProfile = -1;		// -1 => not initialized, 0 => false, 1 => true
	
	ProfileUser m_usrProfile = new ProfileUser();
	ArrayList<ProfileUser> m_lstAllProfiles = null;		// null is not initialized
	
    private SharedPreferences m_settings = null;
    private static SpiderPreferences m_this = null;
}
